package com.changgou;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.*;

/**
 * 实现mysql数据的监听
 */
@CanalEventListener
public class CanalDataEventListener {
    /**
     * 增加数据的监听
     * @param eventType
     * @param rowData
     */
    @InsertListenPoint
    public void onEventInset(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        rowData.getAfterColumnsList().forEach((c) -> System.err.println("列名: " + c.getName() + " :变更的数据:   " + c.getValue()));
    }
    @UpdateListenPoint
    public void onEventUpdate(CanalEntry.RowData rowData) {
        System.err.println("更新操作");
        rowData.getBeforeColumnsList().forEach((c) -> System.err.println("修改前列名: " + c.getName() + " 修改前的数据:   " + c.getValue()));
        rowData.getAfterColumnsList().forEach((c) -> System.err.println("修改后列名: " + c.getName() + " 变更的数据:   " + c.getValue()));
    }

    @DeleteListenPoint
    public void onEventDelete(CanalEntry.EventType eventType,CanalEntry.RowData rowData) {
        System.err.println("删除操作");
        rowData.getBeforeColumnsList().forEach((c) -> System.err.println("删除前列名: " + c.getName() + " 变更的数据:   " + c.getValue()));


    }

    @ListenPoint(destination = "example", schema = "changgou_content", table = {"tb_content"}, eventType = CanalEntry.EventType.UPDATE)
    public void onEvent4(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.err.println("自定义监听");
        rowData.getBeforeColumnsList().forEach((c) -> System.err.println("修改前列名: " + c.getName() + " 修改前的数据:   " + c.getValue()));

        rowData.getAfterColumnsList().forEach((c) -> System.err.println("修改后列名: " + c.getName() + " :修改后数据:   " + c.getValue()));
    }

}
